package com.sex.bull.melancholy.service.outside;

import com.sex.bull.melancholy.service.Const;
import com.sex.bull.melancholy.service.bean.login.UserLoginDetail;
import com.sex.bull.melancholy.service.mybatis.bean.OPHistory;
import com.sex.bull.melancholy.service.mybatis.bean.OPInfo;
import com.sex.bull.melancholy.service.mybatis.service.OPHistoryService;
import com.sex.bull.melancholy.service.mybatis.service.OPInfoService;
import com.sex.bull.melancholy.service.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.JsonUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by LiM on 2017/8/18.
 */
@Service
public class LoginInfoService {

    @Autowired
    OPInfoService opInfoService;
    @Autowired
    OPHistoryService opHistoryService;
    @Autowired
    CommonService commonService;

    private String encryptValue(String sPwd){
        return sPwd;
    }

    /**
     * 新增用户名密码
     * @param sDetail
     * @return
     */
    public Map<String, Object> addOP(String sDetail){
        Map<String, Object> jsonMap = new HashMap<>();
        UserLoginDetail userLoginDetail = JsonUtils.json2Obj(sDetail, UserLoginDetail.class);
        if(userLoginDetail == null){
            commonService.addErrMessage(jsonMap, Const.ERR_NOT_JSON_CODE, Const.ERR_NOT_JSON_MSG);
            return jsonMap;
        }

        OPInfo opInfo = new OPInfo();
        opInfo.setOpName(userLoginDetail.getUserName());
        opInfo.setOpPwd(userLoginDetail.getNewPwd());
        opInfo.setOpStatus((byte) 0);
        opInfo.setCreateTime(new Date());
        opInfo.setIsDelete((byte)0);

        try {
            opInfoService.addData(opInfo);
            commonService.addSuccMessage(jsonMap, null, Const.SUCCESS_ADD_MSG);
        } catch (Exception e) {
            e.printStackTrace();
            commonService.addErrMessage(jsonMap, Const.ERR_USER_LOGIN_ADD_CODE, Const.ERR_USRE_LOGIN_ADD_MSG);
            return jsonMap;
        }
        return jsonMap;
    }

    /**
     * 锁定用户
     * @param sDetail
     * @return
     */
    public Map<String, Object> lockOP(String sDetail){
        Map<String, Object> jsonMap = new HashMap<>();
        UserLoginDetail userLoginDetail = JsonUtils.json2Obj(sDetail, UserLoginDetail.class);
        if(userLoginDetail == null){
            commonService.addErrMessage(jsonMap, Const.ERR_NOT_JSON_CODE, Const.ERR_NOT_JSON_MSG);
            return jsonMap;
        }

        OPInfo opInfo = new OPInfo();
        opInfo.setOpName(userLoginDetail.getUserName());
        opInfo.setOpStatus(Byte.parseByte(userLoginDetail.getPwdStatus()));
        opInfo.setUpdateTime(new Date());

        try {
            opInfoService.updateData(opInfo);
            commonService.addSuccMessage(jsonMap, null, Const.SUCCESS_LOCK_MSG);
        } catch (Exception e) {
            e.printStackTrace();
            commonService.addErrMessage(jsonMap, Const.ERR_USER_LOGIN_LOCK_CODE, Const.ERR_USER_LOGIN_LOCK_MSG);
            return jsonMap;
        }

        return jsonMap;
    }

    /**
     * 逻辑删除用户
     * @param sDetail
     * @return
     */
    public Map<String, Object> deleteOP(String sDetail){
        Map<String, Object> jsonMap = new HashMap<>();
        UserLoginDetail userLoginDetail = JsonUtils.json2Obj(sDetail, UserLoginDetail.class);
        if(userLoginDetail == null){
            commonService.addErrMessage(jsonMap, Const.ERR_NOT_JSON_CODE, Const.ERR_NOT_JSON_MSG);
            return jsonMap;
        }

        OPInfo opInfo = new OPInfo();
        opInfo.setOpName(userLoginDetail.getUserName());
        opInfo.setUpdateTime(new Date());
        opInfo.setIsDelete((byte) 1);

        try {
            opInfoService.deleteData(opInfo);
            commonService.addSuccMessage(jsonMap, null, Const.SUCCESS_DELETE_MSG);
        } catch (Exception e) {
            e.printStackTrace();
            commonService.addErrMessage(jsonMap, Const.ERR_USER_LOGIN_DELETE_CODE, Const.ERR_USER_LOGIN_DELETE_MSG);
            return jsonMap;
        }
        return jsonMap;
    }

    /**
     * 更新用户密码
     * @param sDetail
     * @return
     */
    public Map<String, Object> updateOP(String sDetail){
        Map<String, Object> jsonMap = new HashMap<>();
        UserLoginDetail userLoginDetail = JsonUtils.json2Obj(sDetail, UserLoginDetail.class);
        if(userLoginDetail == null){
            commonService.addErrMessage(jsonMap, Const.ERR_NOT_JSON_CODE, Const.ERR_NOT_JSON_MSG);
            return jsonMap;
        }
        OPInfo opInfo = new OPInfo();
        opInfo.setOpName(userLoginDetail.getUserName());
        opInfo.setIsDelete((byte)0);
        try {
            List<OPInfo> opInfoList = opInfoService.queryAllList(opInfo);
            if(opInfoList == null || opInfoList.size() == 0){
                commonService.addErrMessage(jsonMap, Const.ERR_USER_LOGIN_PWD_CODE, Const.ERR_USER_LOGIN_PWD_MSG);
                return jsonMap;
            }
            opInfo = opInfoList.get(0);

            if(opInfo.getOpPwd().equals(userLoginDetail.getOldPwd())){
                OPHistory opHistory = new OPHistory();
                opHistory.setOpId(opInfo.getId());
                opHistory.setOpPwd(opInfo.getOpPwd());
                opHistory.setCreateTime(new Date());
                opHistory.setIsDelete((byte)0);

                opHistoryService.addData(opHistory);

                opInfo.setOpName(userLoginDetail.getUserName());
                opInfo.setOpPwd(userLoginDetail.getNewPwd());
                opInfo.setOpStatus((byte)0);
                opInfo.setIsDelete((byte)0);
                opInfo.setUpdateTime(new Date());

                opInfoService.updateData(opInfo);
                commonService.addSuccMessage(jsonMap, null, Const.SUCCESS_UPDATE_MSG);

            }else {
                commonService.addErrMessage(jsonMap, Const.ERR_USER_LOGIN_PWD_CODE, Const.ERR_USER_LOGIN_PWD_MSG);
            }

        } catch (Exception e) {
            e.printStackTrace();
            commonService.addErrMessage(jsonMap, Const.ERR_USER_LOGIN_UPDATE_CODE, Const.ERR_USER_LOGIN_UPDATE_MSG);
            return jsonMap;
        }
        return jsonMap;
    }

    /**
     * 验证用户密码
     * @param sDetail
     * @return
     */
    public Map<String, Object> queryOP(String sDetail){
        Map<String, Object> jsonMap = new HashMap<>();
        UserLoginDetail userLoginDetail = JsonUtils.json2Obj(sDetail, UserLoginDetail.class);
        if(userLoginDetail == null){
            commonService.addErrMessage(jsonMap, Const.ERR_NOT_JSON_CODE, Const.ERR_NOT_JSON_MSG);
            return jsonMap;
        }
        OPInfo opInfo = new OPInfo();
        opInfo.setOpName(userLoginDetail.getUserName());
        opInfo.setIsDelete((byte)0);
        try {
            List<OPInfo> opInfoList = opInfoService.queryAllList(opInfo);
            if(opInfoList == null && opInfoList.size() == 0){
                commonService.addErrMessage(jsonMap, Const.ERR_USER_LOGIN_PWD_CODE, Const.ERR_USER_LOGIN_PWD_MSG);
                return jsonMap;
            }

            opInfo = opInfoList.get(0);
            if(opInfo.getOpPwd().equals(userLoginDetail.getOldPwd())){
                if(opInfo.getOpStatus() == 1){
                    commonService.addErrMessage(jsonMap, Const.ERR_USER_LOGIN_QUERY_LOCK_CODE, Const.ERR_USER_LOGIN_QUERY_LOCK_MSG);
                }else {
                    commonService.addSuccMessage(jsonMap, null, Const.SUCCESS_QUERY_MSG);
                }
            }else {
                commonService.addErrMessage(jsonMap, Const.ERR_USER_LOGIN_PWD_CODE, Const.ERR_USER_LOGIN_PWD_MSG);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonMap;
    }

    /**
     * 加密字符串
     * @param sValue
     * @return
     */
    public String encryptPwd(String sValue){
        return encryptPwd(sValue);
    }

}
